package centroidtc;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;

public class FileManager {

	private File avector = null;
	private String outFileName = "out" + File.separator;
	private FileWriter fw = null;
	
	/**
	 * constructor
	 */
	public FileManager(String aFileName) {
		avector = new File(aFileName);
		outFileName += avector.getAbsoluteFile().getName();
		try {
			fw = new FileWriter(outFileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * constructor
	 */
	public FileManager(String aFileName, String categoryName) {
		// mkdir
		String aFolderName = outFileName + categoryName;
		File aFolder = new File(aFolderName);
		if (!aFolder.exists()) {
			aFolder.mkdirs();
		}
		
		outFileName = aFolderName + File.separator + aFileName;
		
		try {
			fw = new FileWriter(outFileName);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * write an array into outfile
	 */
	public void writeArrayIntoFile(ArrayList arr, String seperator){
		if (arr == null) {
			return;
		}
		String sep = "";
		if (seperator == null || seperator.equalsIgnoreCase("")) {
			sep = ",";
		}else{
			sep = seperator;
		}
		
		// ping a big string
		String aBigString = "";
		for (int i = 0; i < arr.size(); i++) {
			aBigString += arr.get(i) + sep;
		}
		
		try {
			fw.write(aBigString + "\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * write an sorted array into outfile
	 */
	public void writeSortedArrayIntoFile(ArrayList arr, String seperator){
		if (arr == null) {
			return;
		}
		String sep = "";
		if (seperator == null || seperator.equalsIgnoreCase("")) {
			sep = ",";
		}else{
			sep = seperator;
		}
		// sort the array
		Object[] inn = new Object[arr.size()];
		for (int i = 0; i < inn.length; i++) {
			inn[i] = arr.get(i);
		}
		Arrays.sort(inn);
		// ping a big string
		String aBigString = "";
		for (int i = 0; i < inn.length; i++) {
			aBigString += inn[i] + sep;
		}
		
		try {
			fw.write(aBigString + "\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * write a map into outfile
	 */
	public void writeMapIntoFile(Map aMap, String seperator){
		if (aMap == null) {
			return;
		}
		ArrayList tmpArray = new ArrayList();
		for (Iterator iter = aMap.entrySet().iterator(); iter.hasNext();) {
			Map.Entry pp = (Map.Entry) iter.next();
			tmpArray.add(pp.getValue());
		}
		writeArrayIntoFile(tmpArray,seperator);
		tmpArray.clear();
	}
	
	/**
	 * write a big string into outfile
	 */
	public void writeBigStringIntoFile(String aBigString){
		if (aBigString == null) {
			return;
		}
		try {
			fw.write(aBigString + "\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * close an writer
	 */
	public void closeWriter(){
		try {
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {

	}
}
